package com.tgy.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tgy.admin.Service.AdminFileService;
import com.tgy.page.PageQuery;
import com.tgy.pojo.BlogFile;
import com.tgy.result.GraceJSONResult;
import com.tgy.result.LayuiTableResult;
import com.tgy.util.AppFileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/6/21 21:01
 */
@Validated
@Api(value = "资源文件控制器", tags = {"资源文件管理"})
@RestController
@RequestMapping("/admin/file")
public class AdminFileController {

    private final static Logger logger = LoggerFactory.getLogger(AdminFileController.class);

    private static Map<String,String> images = new HashMap<>();

    private static Set<String> imageSet;
    static {
        // key文件类型，value为图片名称
        images.put(".xlsx,.xls","excel.png");
        images.put(".exe","exe.png");
        images.put(".jpg,.png,.gif","image.png");
        images.put(".mp3,.aac,.wma","mp3.png");
        images.put(".mp4,.avi,.wmv,.mpeg","mp4.png");
        images.put(".pdf","pdf.png");
        images.put(".ppt,.pptx","ppt.png");
        images.put(".docx,doc","world.png");
        images.put(".gz,.zip,.7z,.rar,.bz","zip.png");
        images.put("unknown","unknown.png");

        imageSet = images.keySet();
    }

    @Resource
    private AdminFileService adminFileService;

    @ApiOperation("获取资源文件列表")
    // @SaCheckPermission
    @GetMapping("getCategoryList")
    public LayuiTableResult getCategoryList(BlogFile bo, PageQuery pageQuery){
        return adminFileService.getBlogFileList(bo,pageQuery);
    }

    @ApiOperation("添加资源文件")
    @SaCheckPermission("blogFile:add")
    @PostMapping("addBlogFile")
    public GraceJSONResult addBlogFile(MultipartFile mf){
        Map<String, String> map;
        try {
            map = AppFileUtils.uploadFile(mf);
        }catch (Exception e){
            logger.error("上传文件失败"+e.getMessage());
            return GraceJSONResult.error();
        }
        if (Objects.isNull(map)){
            return GraceJSONResult.error();
        }else {
            try {
                // 保存数据库信息
                String oldFileName = map.get("oldFileName");
                String[] split = StringUtils.split(oldFileName , "==");
                BlogFile blogFile = new BlogFile();

                blogFile.setFileName(map.get("newFileName"));
                blogFile.setFileType(map.get("stuff"));
                blogFile.setFilePath(map.get("path"));
                blogFile.setTitle(split[0]);
                blogFile.setRemark(split[2]+"，"+split[1]);

                String imageUrl = "../../images/filetype/";
                String image = "unknown.png";
                for (String key : imageSet) {
                    if (key.contains(map.get("stuff"))){
                        image = images.get(key);
                        break;
                    }
                }
                blogFile.setImage(imageUrl+image);
                return adminFileService.addBlogFile(blogFile);
            }catch (Exception e){
                e.printStackTrace();
                // 删除文件
                AppFileUtils.removeFileByPath(map.get("path"));
                return GraceJSONResult.error();
            }

        }

    }

    @ApiOperation("修改资源文件")
    @SaCheckPermission("blogFile:update")
    @PostMapping("updateBlogFile")
    public GraceJSONResult updateBlogFile(BlogFile BlogFile){
        return adminFileService.updateBlogFile(BlogFile);
    }

    @ApiOperation("删除资源文件")
    @SaCheckPermission("blogFile:del")
    @PostMapping("delBlogFile")
    // @PostMapping("delBlogFile/{id}")
    public GraceJSONResult delBlogFile(@ApiParam("ID必须传入") String id){
        return adminFileService.delBlogFile(id);
    }

    @ApiOperation("批量删除资源文件")
    @SaCheckPermission("blogFile:del")
    @PostMapping("delBlogFileIds")
    public GraceJSONResult delBlogFileIds(@ApiParam("ids必须传入") String ids){
        List<String> list = new ArrayList<>(Arrays.asList(ids.split(",")));
        return adminFileService.delBlogFileIds(list);
    }

    @ApiOperation("根据ID查询资源文件")
    @SaCheckPermission
    @PostMapping("getBlogFileById")
    public GraceJSONResult getBlogFileById(@ApiParam("ID必须传入") String id){
        return adminFileService.getBlogFileById(id);
    }


}
